package com.cyy.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cyy.common.ResData;
import com.cyy.controller.vo.RequestTransactionVo;
import com.cyy.service.RpcService;
import com.cyy.service.RpcServiceHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/")
public class ApiController extends BaseController{


    /**
     * 交易确认
     * @param coin  币的名称
     * @param hexString 32个字节 - 交易的哈希 (返回有关交易哈希请求的交易的信息)
     * @return
     */
    @RequestMapping("getTransactionByHash/{coin}")
    public ResData<JSONObject> getTransactionByHash(@PathVariable("coin")String coin,
                                                    @RequestParam("hexString")String hexString){
        JSONObject result = this.getRpcServiceByName(coin).getTransactionByHash(hexString);
        return ResData.success(result);
    }

    /**
     * 返回地址
     * @param coin
     * @return
     */
    @RequestMapping("getAccount/{coin}")
    public ResData<JSONArray> getAccount(@PathVariable("coin")String coin){
        JSONArray result = this.getRpcServiceByName(coin).getAccount();
        return ResData.success(result);
    }

    /**
     * 返回越
     * @param coin
     * @param address
     */
    @RequestMapping("getBalance/{coin}")
    public ResData<String> getBalance(@PathVariable("coin")String coin,
                                      @RequestParam("address")String address){
        String result = this.getRpcServiceByName(coin).getBalance(address);
        return ResData.success(result);
    }




    @RequestMapping(value = "sendTransaction/{coin}",method = RequestMethod.POST)
    public ResData<String> sendTransaction(@PathVariable("coin")String coin,
                                           @RequestBody RequestTransactionVo vo){
        String result = this.getRpcServiceByName(coin).sendTransaction(vo.getToAddr(),vo.getPayAddr(),vo.getPrice(),vo.getMinerFee());
        return ResData.success(result);
    }


}
